package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.DepartmentDto;

/**
 * 部门
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class DepartmentJdbcDao implements DepartmentDao {
	@Override
	public int deleteByIds(final DepartmentDto deleteDepartment) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteDepartment.getAttrs())) {
			model.putAll(deleteDepartment.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteDepartment.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final DepartmentDto findDepartment) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDepartment, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findDepartment, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findDepartment.getAttrs())) {
			model.putAll(findDepartment.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final DepartmentDto findDepartment) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDepartment);

		final Model model = ModelUtils.newModel(new ModelArgs(findDepartment, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findDepartment.getAttrs())) {
			model.putAll(findDepartment.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final DepartmentDto findListDepartment) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListDepartment).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListDepartment.getAttrs())) {
			model.putAll(findListDepartment.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final DepartmentDto insertDepartment) {
		if (StringUtils.isBlank(insertDepartment.getId())) {
			insertDepartment.setId(UUID.randomUUID().toString());
		} else {

		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertDepartment));

		if (MapUtils.isNotEmpty(insertDepartment.getAttrs())) {
			model.putAll(insertDepartment.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final DepartmentDto paginationDepartment) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationDepartment).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationDepartment.getAttrs())) {
			model.putAll(paginationDepartment.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationDepartment), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final DepartmentDto readDepartment) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readDepartment, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readDepartment.getAttrs())) {
			model.putAll(readDepartment.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readDepartment);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readDepartment, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final DepartmentDto saveDepartment) {
		final P oldDepartment = this.findByCodeOrId(mappedClass, saveDepartment);

		if (oldDepartment == null) {
			this.insertByCode(saveDepartment);

			return this.findByCodeOrId(mappedClass, saveDepartment);
		}

		final int updateCount = this.updateByCode(saveDepartment, oldDepartment);

		if (updateCount < 1) {
			return oldDepartment;
		}

		return this.findByCodeOrId(mappedClass, saveDepartment);
	}

	@Override
	public int updateByCode(final DepartmentDto updateDepartment) {
		final DepartmentDto oldDepartment = this.findByCodeOrId(DepartmentDto.class, updateDepartment);

		return this.updateByCode(updateDepartment, oldDepartment);
	}

	private <P> int updateByCode(final DepartmentDto updateDepartment, final P oldDepartment) {
		if (oldDepartment == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateDepartment);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateDepartment, oldDepartment, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldDepartment, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldDepartment, updateDepartment, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateDepartment, updateProperties), new ModelArgs(updateDepartment, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateDepartment.getAttrs())) {
			model.putAll(updateDepartment.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}